package com.robot.backend.controller.record;


import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.robot.backend.service.record.recordCRUDService;

import java.util.Map;

@RestController
@RequestMapping("/recordCRUD/")
public class recordCRUDController {
    @Autowired
    recordCRUDService recordCRUDService;

    @PostMapping("add/")
    public boolean addRecord(Map<String,String> map){
        int aid = Integer.parseInt(map.get("aid"));
        int bid = Integer.parseInt(map.get("bid"));
        int abot = Integer.parseInt(map.get("abot"));
        int bbot = Integer.parseInt(map.get("bbot"));
        String loser =map.get("loser");
        int asx = Integer.parseInt(map.get("asx"));
        int asy = Integer.parseInt(map.get("asy"));
        int bsx = Integer.parseInt(map.get("bsx"));
        int bsy = Integer.parseInt(map.get("bsy"));
        int winScore = Integer.parseInt(map.get("winScore"));
        int loseScore = Integer.parseInt(map.get("loseScore"));
        int[] asteps = map.get("asteps").chars().map(Character::getNumericValue).toArray();
        int[] bsteps = map.get("bsteps").chars().map(Character::getNumericValue).toArray();;
        int[][] gameMap = new int[15][15];//字符串转二维数组
        for (int i = 0; i < 13; i++) {
            for (int j = 0; j < 14; j++) {
                // 计算当前字符在字符串中的索引位置
                int index = i * 14 + j;
                // 将字符转换为数字并填充到数组中
                gameMap[i][j] = Character.getNumericValue(map.get("gameMap").charAt(index));
            }
        }
        return recordCRUDService.addRecord(aid,bid,abot,bbot,loser,asteps,bsteps,gameMap,asx,asy,bsx,bsy,winScore,loseScore);
    }
    @PostMapping("getRecordById/")
    public JSONObject getRecordById(@RequestParam Map<String,String> map){
        int id = StpUtil.getLoginIdAsInt();
        int page = Integer.parseInt(map.get("page"));
        int sizes = Integer.parseInt(map.get("sizes"));
        System.out.println(map);
        return recordCRUDService.getRecordById(id,page,sizes);
    }
    @PostMapping("getAllRecord/")
    public JSONObject getAllRecord(@RequestParam Map<String,String> map){
        int page = Integer.parseInt(map.get("page"));
        int sizes = Integer.parseInt(map.get("sizes"));
        return recordCRUDService.getAllRecord(page,sizes);
    }

}
